package vn.edu.fpt.taskmanager;

import org.apache.log4j.Logger;

import vn.edu.fpt.taskmanager.tasks.Task;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * A class contains code for AddTask Screen.
 * @author GaSjeuThjt
 */
public class AddTaskActivity extends TaskManagerActivity {

	private Logger logger = Logger.getLogger(AddTaskActivity.class);
	
	private static Activity addTaskActivity;
	
	private Button btnCancel;
	private Button btnAddTask;
	private EditText edtTaskName;

	/**
	 * Get reference to controls (buttons, textviews ...) and add necessarry listeners.
	 * @author GaSjeuThjt
	 */
	private void setUpView() {
		
		setContentView(R.layout.activity_addtask);
		
		addTaskActivity = this;
		
		btnCancel = (Button) findViewById(R.id.cancel_button);
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				cancel();
			}
		});
	
		btnAddTask = (Button) findViewById(R.id.addtask_button);
		btnAddTask.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				addTask();
			}
		});
	
		edtTaskName = (EditText) findViewById(R.id.taskname_edittext);
	}
	
	/**
	 * Get called when Cancel button of AddTask screen is clicked.
	 * @author GaSjeuThjt
	 */
	protected void cancel() {
		finish();
		
		// Logging
		logger.info("Cancel button is tapped. AddTaskActivity is finished");
	}
	
	/**
	 * Get called when AddTask button is clicked.
	 * @author GaSjeuThjt
	 */
	protected void addTask() {
		
		TaskManagerApplication tma = getTaskManagerApplication();
		tma.addTask(new Task(edtTaskName.getText().toString()));
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setUpView();
		
		// Logging
		logger.info("View is setup for AddTaskActivity");
	}

	protected static Activity getActivity() {
		return addTaskActivity;
	}
}
